import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.awt.event.*;
import java.util.Random;


public class RiverCrossingView extends JFrame implements ActionListener {

	private InventoryModel im;
	private JPanel contentPane;
	private ImageIcon riverIcon;
	private JLabel riverCrossing;
	private DisasterView dv = new DisasterView();
	private Random rand = new Random();

	/**
	 * Create the frame.
	 */
	public RiverCrossingView(InventoryModel im) {
		this.im = im;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		riverIcon = new ImageIcon("riverCrossing.jpg");
		riverCrossing = new JLabel(new ImageIcon("riverCrossing.jpg"));
		contentPane = new JPanel();
		contentPane.setBackground(new Color(218, 165, 32));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnNewButton = new JButton("Detour");
		btnNewButton.addActionListener(this);
		
		JButton btnNewButton_1 = new JButton("Water Tires");
		btnNewButton_1.addActionListener(this);
		
		JButton btnNewButton_2 = new JButton("Cross");
		btnNewButton_2.addActionListener(this);

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(60, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addGap(53)
					.addComponent(btnNewButton_1)
					.addGap(42)
					.addComponent(btnNewButton_2)
					.addGap(56))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(riverCrossing, GroupLayout.PREFERRED_SIZE, 395, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(19, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(riverCrossing, GroupLayout.PREFERRED_SIZE, 200, Short.MAX_VALUE)
					.addGap(7)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_2)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton))
					.addContainerGap())
		);
		contentPane.setLayout(gl_contentPane);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Detour")) {
			if (rand.nextInt(3) == 2 && im.getItems().get(1).getQuantity() >= 3) {
				im.getItems().get(1).subtractQuantity(3);
				JOptionPane.showMessageDialog(this, "Oh no! You hit a pothole on the detour and 3 of your food packages fell out of your car.");
			} else {
				JOptionPane.showMessageDialog(this, "You made it through the detour with no problems.");
			}
		} else if (e.getActionCommand().equals("Water Tires")) {
			if (rand.nextInt(100) == 99) {
				JOptionPane.showMessageDialog(this, "Well. Turns out your water tires were infested with termites and had holes in them. Your car sank and everyone died. Game over.");
			} else if (rand.nextInt(3) == 2 && im.getItems().get(1).getQuantity() >= 5) {
				im.getItems().get(1).subtractQuantity(5);
				JOptionPane.showMessageDialog(this, "No way! Water leaked into your some of your food. Five of your food packages cannot be eaten.");
			} else {
				JOptionPane.showMessageDialog(this, "You made it across the river with no problems.");
			}
		} else if (e.getActionCommand().equals("Cross")) {
			if (rand.nextInt(3) == 2 && im.getItems().get(1).getQuantity() >= 7) {
				im.getItems().get(1).subtractQuantity(7);
				JOptionPane.showMessageDialog(this, "Oh no! You hit a pothole on the bridge and 7 of your food packages fell out of your car.");
			} else {
				JOptionPane.showMessageDialog(this, "You made it across the river with no problems.");
			}
		}
		setVisible(false);
	}

}
